package me.raih.controller.page;

import me.raih.pojo.Menu;
import me.raih.service.MenuService;
import me.raih.utils.MenuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.List;
import java.util.Objects;

/**
 * @author Raih
 * @version 1.8
 * @Time 2020/9/10  15:41
 */
@Controller
@SessionAttributes(value = "MenuObjectSession")
public class IndexController {

    @Autowired
    MenuService menuService;


    @RequestMapping(value = "/index")
    public String toIndex(Model model, ModelMap modelMap){

        Object menuSession = modelMap.getAttribute("MenuObjectSession");
        if(Objects.isNull(menuSession)){
            //如果是空的，说明不存在，从数据库中获取
            List<Menu> menuList = menuService.findAllMenu();
            List<Object> okList = MenuUtil.getMenuList(menuList);
            System.out.println("第一次，session中是没有的！");
            //并存在session中
            model.addAttribute("MenuObjectSession",okList);
        }
        else {
            System.out.println("Session中有，直接拿吧");
        }

        return "index";
    }

}
